package cn.hicard.risk.service.bd.impl;

import java.util.List;

import org.springframework.stereotype.Service;

import cn.hicard.auth.dao.UserRoleDao;
import cn.hicard.auth.dao.UserinfoDao;
import cn.hicard.auth.entity.MgUserRole;
import cn.hicard.auth.entity.MgUserinfo;
import cn.hicard.core.Page;
import cn.hicard.core.PageResult;
import cn.hicard.risk.dao.bd.JzgRiskestimateDao;
import cn.hicard.risk.entity.JzgRiskestimate;
import cn.hicard.risk.service.bd.JzgRiskestimateService;
import cn.hicard.util.DateHelp;
import cn.hicard.util.RoleCheck;

/**
 * 金掌柜特约商户风险评估数据业务接口实现类
 * @author zhangy
 *
 */
@Service
public class JzgRiskestimateServiceImpl implements JzgRiskestimateService {
	
	private JzgRiskestimateDao jzgRiskestimateDao;
	
	public void setJzgRiskestimateDao(JzgRiskestimateDao jzgRiskestimateDao) {
		this.jzgRiskestimateDao = jzgRiskestimateDao;
	}

	private UserinfoDao userinfoDao;
	
	public void setUserinfoDao(UserinfoDao userinfoDao) {
		this.userinfoDao = userinfoDao;
	}

	private UserRoleDao userRoleDao;

	public void setUserRoleDao(UserRoleDao userRoleDao) {
		this.userRoleDao = userRoleDao;
	}

	@Override
	public void saveEstimate(JzgRiskestimate estimate)throws Exception{
		estimate.setAdddatetime(DateHelp.dateTime());
		estimate.setModdatetime(DateHelp.dateTime());
		jzgRiskestimateDao.saveEstimate(estimate);
	}

	@Override
	public JzgRiskestimate findEstimate(int estimateId) throws Exception {
		return jzgRiskestimateDao.findEstimate(estimateId);
	}

	@Override
	public PageResult<JzgRiskestimate> searchEstimate(
			JzgRiskestimate estimateStart, 
			JzgRiskestimate estimateEnd, 
			Page page) throws Exception{
		
		MgUserinfo user = userinfoDao.getByLoginid(estimateStart.getAddloginid());
		List<MgUserRole> urs = userRoleDao.getByUser(user.getPkUserinfo());
		// 是否拥有“复审角色”判定
		for (MgUserRole ur : urs) {
			// 判断[角色]主键当是否为“金掌柜复审”角色的主键
			if(RoleCheck.isJzgRecheck(ur.getId().getPkRoleinfo())){
				// 如果是 金掌柜商户风险评估-复审员   则去除用户限制
				estimateStart.setAddloginid(null);
				break;
			}				
		}
		return jzgRiskestimateDao.searchEstimate(estimateStart,estimateEnd,page);
	}

	@Override
	public void modifyEstimate(JzgRiskestimate estimate) throws Exception {
		estimate.setModdatetime(DateHelp.dateTime());
		jzgRiskestimateDao.modifyEstimate(estimate);
	}

}
